package com.item.springsecurity.config;


import com.item.springsecurity.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;

/**
 * _______                             __________
 * \      \   _______  __ ___________  \______   \__ __  ____
 * /   |   \_/ __ \  \/ // __ \_  __ \  |    |  _/  |  \/ ___\
 * /    |    \  ___/\   /\  ___/|  | \/  |    |   \  |  / /_/  >
 * \____|__  /\___  >\_/  \___  >__|     |______  /____/\___  /
 * \/     \/          \/                \/     /_____/
 *
 * @Auther: wangdong
 * @Description: TODO
 * @Date: 2019/4/23 14:05
 * @Version 1.0
 */

public class MyUserDetailsService extends User implements UserDetails {

    Logger logger = LoggerFactory.getLogger(getClass());

    private static final long serialVersionUID = 1L;

    public MyUserDetailsService(User user) {
        if (user != null) {
            this.setUser_uuid(user.getUser_uuid());
            this.setUsername(user.getUsername());
            this.setPassword(user.getPassword());
            this.setEmail(user.getEmail());
            this.setTelephone(user.getTelephone());
            this.setRole(user.getRole());
            this.setImage(user.getImage());
            this.setLast_ip(user.getLast_ip());
            this.setLast_time(user.getLast_time());
        }
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        Collection<GrantedAuthority> authorities = new ArrayList<>();
        String username = this.getUsername();
        logger.info("登陆用户:"+username);
        if (username != null) {
            SimpleGrantedAuthority authority = new SimpleGrantedAuthority(username);
            authorities.add(authority);
            logger.info("没有此用户");
        }
        return authorities;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }


}
